//
//  NoticeViewController.m
//  Mid_Project
//
//  Created by 王世界 on 2020/11/30.
//

#import "NoticeViewController.h"
#import "Single.h"
#import "AFNetworking.h"
#import "Masonry.h"

#import <PPBadgeView.h>
#import "CustomScrollView.h"

@interface NoticeViewController ()

@end

@implementation NoticeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated{
    self.view.backgroundColor = [UIColor colorWithRed:242.0/255 green:242.0/255 blue:247.0/255 alpha:1];
    self.Data = [Single sharedInstance].datalist1[1];
    NSLog(@"%@",self.Data);
    [self.tableView reloadData];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = UIColor.clearColor;
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view).multipliedBy(0.9);
        make.top.equalTo(self.view.mas_top).with.offset(self.view.frame.size.height*0.1);
        make.bottom.equalTo(self.view.mas_bottom).with.offset(-self.view.frame.size.height*0.1);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.Data.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 20;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0, 320,5)];
    view.backgroundColor = [UIColor clearColor];
    
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0, 320,5)];
    view.backgroundColor = [UIColor clearColor];
//    [view.bounds];
    return view;
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle  reuseIdentifier:@"reuse"];
    NSString *t = [NSString stringWithFormat:@"%@",self.Data[indexPath.section][@"Data"][@"Type"]];
    if ([t isEqualToString:@"reply"]) {
        NSString *string = [NSString stringWithFormat:@"%@ %@",self.Data[indexPath.section][@"User"][@"Name"],@"对你进行了评论"];
        cell.textLabel.text = string;
        NSString *str2 = [NSString stringWithFormat:@"%@", self.Data[indexPath.section][@"Data"][@"Content"]];
        
            cell.detailTextLabel.text = str2;
        NSString *str3 = [NSString stringWithFormat:@"%@", self.Data[indexPath.section][@"Data"][@"Read"]];
        if ([str3 isEqualToString:@"0"]) {
            [cell pp_addDotWithColor:[UIColor redColor]];
            [cell pp_setBadgeHeight:8];
            [cell pp_moveBadgeWithX:-20 Y:15];
        }
        else {
            [cell pp_addDotWithColor:nil];
        }
    }
    else {
        NSString *string = [NSString stringWithFormat:@"%@ %@",self.Data[indexPath.section][@"User"][@"Name"],@"赞了你"];
        cell.detailTextLabel.text = string;
        NSString *str3 = [NSString stringWithFormat:@"%@", self.Data[indexPath.section][@"Data"][@"Read"]];
        if ([str3 isEqualToString:@"0"]) {
            [cell pp_addDotWithColor:[UIColor redColor]];
            [cell pp_setBadgeHeight:8];
            [cell pp_moveBadgeWithX:-20 Y:15];
        }
        else {
            [cell pp_addDotWithColor:nil];
        }
    }
    cell.textLabel.font = [UIFont systemFontOfSize:16];
    cell.textLabel.textColor = [UIColor grayColor];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:16];
    cell.indentationLevel = 1;
    [cell.layer setMasksToBounds:YES];
    
    cell.layer.cornerRadius=5;
    cell.layer.borderWidth = 1;
//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *url3;
    
        url3 = [NSString stringWithFormat:@"%@%@", @"http://172.18.178.56/api/notification/read/",self.Data[indexPath.section][@"Data"][@"ID"]];
    
    
    NSDictionary *parameters = @{@"Read": @"true"};
    AFHTTPSessionManager *manage3 = [AFHTTPSessionManager manager];
    // 设置请求体为JSON
    manage3.requestSerializer = [AFJSONRequestSerializer serializer];
    // 设置响应体为JSON
    manage3.responseSerializer = [AFJSONResponseSerializer serializer];
    [manage3 PATCH:url3 parameters:parameters headers:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 60;
  
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    //处理删除的操作
    NSString *url3;
    
        url3 = [NSString stringWithFormat:@"%@%@", @"http://172.18.178.56/api/notification/",self.Data[indexPath.section][@"Data"][@"ID"]];
    
    
    AFHTTPSessionManager *manage3 = [AFHTTPSessionManager manager];
    // 设置请求体为JSON
    manage3.requestSerializer = [AFJSONRequestSerializer serializer];
    // 设置响应体为JSON
    manage3.responseSerializer = [AFJSONResponseSerializer serializer];
    [manage3 DELETE:url3 parameters:nil headers:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSString *url4 = @"http://172.18.178.56/api/notification/all";
        
        AFHTTPSessionManager *manage3 = [AFHTTPSessionManager manager];
        // 设置请求体为JSON
        manage3.requestSerializer = [AFJSONRequestSerializer serializer];
        // 设置响应体为JSON
        manage3.responseSerializer = [AFJSONResponseSerializer serializer];
        [manage3 GET:url4 parameters:nil headers:nil progress:^(NSProgress * _Nonnull uploadProgress){} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *returndata = (NSDictionary*)responseObject;
            self.Data = [[NSMutableArray alloc] init];
            self.Data = (NSMutableArray*)returndata[@"Notification"];
            [self.tableView reloadData];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        }];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}


@end

